package com.mmlc.inter;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * @param <T>
 * @author king
 * @function PagerAdapter通用类
 * @date 2016-1-14上午9:41:53
 */
public class ViewPagerAdapter<T extends View> extends PagerAdapter {

    private final List<T> views;
    public ViewPagerAdapter(List<T> views) {
        super();
        this.views = views;
    }


    // viewpager中的组件数量
    @Override
    public int getCount() {
        return views.size();
    }

    // 滑动切换的时候销毁当前的组件
    @SuppressWarnings("unchecked")
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }

    // 每次滑动的时候生成的组件
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (container.getParent() != null)
            ((ViewGroup) container.getParent()).removeView(views.get(position));
        ((ViewPager) container).addView(views.get(position));
        return views.get(position);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

}
